
package uk.ltd.pomegranate.wii;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import java.util.logging.Level;

import org.jdesktop.application.Application;
import org.jdesktop.application.Application.ExitListener;

import wiiremotej.WiiRemote;
import wiiremotej.WiiRemoteJ;
import wiiremotej.event.WRButtonEvent;
import wiiremotej.event.WRAccelerationEvent;
import wiiremotej.event.WRExtensionEvent;
import wiiremotej.event.WRStatusEvent;
import wiiremotej.event.WiiDeviceDiscoveredEvent;
import wiiremotej.event.WiiDeviceDiscoveryListener;
import wiiremotej.event.WiiRemoteAdapter;

public class WiimoteDataHandler extends WiiRemoteAdapter implements ExitListener, WiiDeviceDiscoveryListener {
	
	private List<WiiRemote> wiiRemotes = new ArrayList<WiiRemote>();
	
	public WiimoteDataHandler() {
			WiiRemoteJ.findRemotes( this,1);
			Application.getInstance().addExitListener( this );		
	}
	
	void addRemote(final WiiRemote remote) {
		try {
			int id = wiiRemotes.size();
			remote.setAccelerometerEnabled(true);
//			remote.setIRSensorEnabled(true, WRIREvent.BASIC, SENSITIVITY_BLOCK1, SENSITIVITY_BLOCK2);
			remote.setLEDIlluminated(id, true);			
			wiiRemotes.add( remote );
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		remote.addWiiRemoteListener(this);

	}
	

	/////////////////////////
	// ExitListener
	/////////////////////////
	public void willExit(EventObject event) {
		for (WiiRemote remote : wiiRemotes)
			remote.disconnect();
	}
	
	public boolean canExit(EventObject event) {
		return true;
	}

	
	//////////////////////////
	// WiiRemoteAdapter
	//////////////////////////
	
	@Override
	public void accelerationInputReceived(WRAccelerationEvent evt) {
/*		WiiMan.log( Level.INFO, "Pitch: "+evt.getPitch());
		WiiMan.log( Level.INFO, "Roll:  "+evt.getRoll());
		WiiMan.log( Level.INFO, "XAcceleration: "+evt.getXAcceleration());
		WiiMan.log( Level.INFO, "YAcceleration: "+evt.getYAcceleration());
		WiiMan.log( Level.INFO, "ZAcceleration: "+evt.getZAcceleration());
		WiiMan.log( Level.INFO, "isStill: "+evt.isStill());
*/	}
	
	@Override
	public void buttonInputReceived(WRButtonEvent e) {
		if (e.isOnlyPressed(WRButtonEvent.A)) {
			WiiMan.log( Level.INFO,"buttonInputReceived: A");
		} else if (e.isOnlyPressed(WRButtonEvent.HOME)) {
			WiiMan.log( Level.INFO,"buttonInputReceived: Home");
			Application.getInstance().exit();
		}
	}
	
	@Override
	public void extensionInputReceived( WRExtensionEvent e ) {
		WiiMan.log( Level.INFO,"extensionInputReceived: "+e.toString() );
	}

	@Override
	public void statusReported( WRStatusEvent e ) {
		WiiMan.log( Level.INFO,"statusReported: "+e.toString() );
	}

	@Override
	public void disconnected() {
		WiiRemoteJ.stopFind();
		for (WiiRemote remote : wiiRemotes) {
			if (!remote.isConnected()) {
				wiiRemotes.remove( remote );
				break;
			}
		}
	}	
	
	@Override
	public void wiiDeviceDiscovered(WiiDeviceDiscoveredEvent e) {
		if (e.getWiiDevice() instanceof WiiRemote) {
			addRemote((WiiRemote)e.getWiiDevice());
			WiiRemoteJ.stopFind();
		}
	}
	
	/////////////////////////////////
	// WiiDeviceDiscoveryListener
	/////////////////////////////////
	
	@Override
	public void findFinished(int arg0) {
		// TODO Auto-generated method stub
		WiiMan.log( Level.INFO, "findFinished( .. )");
	}	

	
	///////////////////////////////
	// Getter & Setter
	///////////////////////////////
	
	public boolean isConnected() {
		for (WiiRemote remote : wiiRemotes) {
			if (remote.isConnected()) return true;
		}
		return false;
	}
	
	public int getNumberOfConnectedWiimotes() {
		return wiiRemotes.size();
	}
	
}
